package com.uaf.vxalipaybill.provider.utils;

import java.io.InputStream;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.springframework.stereotype.Component;

import com.uaf.log.MySlf4j;
import com.uaf.vxalipaybill.api.exception.VxAlipayBillException;
import com.uaf.vxalipaybill.api.exception.VxAlipayBillServiceException;

/**
 * @filename EmailUtils.java
 * @description 邮箱工具类
 * @author 马庆智
 * @date 2021/9/15 11:06
 */
@Component
public class EmailUtils {

	/**
	 * 功能描述 判断是否有附件
	 * @param messages 邮箱信息
	 * @return boolean 返回true 或者 false
	 * @author 马庆智
	 * @date 2021/9/7 15:13
	 */
	public static boolean getContainerAttachment(Message... messages) throws Exception {
		if (messages == null || messages.length < 1) {
			throw new VxAlipayBillServiceException(VxAlipayBillException.ERR_100002);
		}
		/** 解析所有邮件*/
		for (int i = 0, count = messages.length; i < count; i++) {
			MimeMessage msg = (MimeMessage) messages[i];
			boolean isContainerAttachment = isContainAttachment(msg);
			MySlf4j.textInfo("是否包含附件:{0}", isContainerAttachment);
			if (isContainerAttachment) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 功能描述 判断是否含附件
	 * @param part 邮箱信息
	 * @return boolean 返回true 或者 false
	 * @author 马庆智
	 * @date 2021/9/7 15:13
	 */
	public static boolean isContainAttachment(Part part) throws Exception {
		boolean flag = false;
		if (part.isMimeType("multipart/*")) {
			MimeMultipart multipart = (MimeMultipart) part.getContent();
			int partCount = multipart.getCount();
			for (int i = 0; i < partCount; i++) {
				BodyPart bodyPart = multipart.getBodyPart(i);
				String disp = bodyPart.getDisposition();
				if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
					flag = true;
				} else if (bodyPart.isMimeType("multipart/*")) {
					flag = isContainAttachment(bodyPart);
				} else {
					String contentType = bodyPart.getContentType();
					if (contentType.indexOf("application") != -1) {
						flag = true;
					}

					if (contentType.indexOf("name") != -1) {
						flag = true;
					}
				}

				if (flag)
					break;
			}
		} else if (part.isMimeType("message/rfc822")) {
			flag = isContainAttachment((Part) part.getContent());
		}
		return flag;
	}

	/**
	 * 功能描述   保存附件并返回io流
	 * @param part 邮箱信息
	 * @param message 邮箱信息
	 * @param idValue  每份邮件对应的唯一标识idValue
	 * @return java.io.InputStream  返回io流
	 * @author 马庆智
	 * @date 2021/9/9 13:13
	 */
	public static InputStream saveAttachment(Part part, String message, String idValue) throws Exception {
		if (part.isMimeType("multipart/*")) {
			Multipart multipart = (Multipart) part.getContent();    //复杂体邮件
			//复杂体邮件包含多个邮件体
			int partCount = multipart.getCount();
			for (int i = 0; i < partCount; i++) {
				//获得复杂体邮件中其中一个邮件体
				BodyPart bodyPart = multipart.getBodyPart(i);
				//某一个邮件体也有可能是由多个邮件体组成的复杂体
				String disp = bodyPart.getDisposition();
				if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
					InputStream is = bodyPart.getInputStream();
					return is;
				}
			}
		}
		return null;
	}

	/**
	 * 功能描述 获得邮件发件地址
	 * @param msg 邮件内容
	 * @return java.lang.String 邮箱地址
	 * @author 马庆智
	 * @date 2021/9/9 13:16
	 */
	public static String getAddressUrl(MimeMessage msg) throws Exception {
		String from = "";
		Address[] froms = msg.getFrom();
		if (froms.length < 1)
			throw new VxAlipayBillServiceException(VxAlipayBillException.ERR_100002);

		InternetAddress address = (InternetAddress) froms[0];
		from = address.getAddress();
		return from;
	}

	/**
	 * 功能描述  文本解码
	 * @param encodeText 解码MimeUtility.encodeText(String text)方法编码后的文本
	 * @return java.lang.String 解码后的文本
	 * @author 马庆智
	 * @date 2021/9/9 13:17
	 */
	public static String decodeText(String encodeText) throws Exception {
		if (encodeText == null || "".equals(encodeText)) {
			return "";
		} else {
			return MimeUtility.decodeText(encodeText);
		}
	}

}
